package com.itbaizhan.shopping_goods_service.service.impl;



import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.*;
import com.itbaizhan.shopping_common.service.GoodsService;
import com.itbaizhan.shopping_goods_service.mapper.GoodsImageMapper;
import com.itbaizhan.shopping_goods_service.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImageMapper goodsImageMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);

        //商品图片信息
        List<GoodsImage> images = goods.getImages();
        //添加图片
        for (GoodsImage image : images) {
            image.setGoodsId(goods.getId());
            goodsImageMapper.insert(image);
        }

        List<Specification> specifications = goods.getSpecifications();
        for (Specification specification : specifications) {
            List<SpecificationOption> optionList = specification.getSpecificationOptions();
            for (SpecificationOption option : optionList) {
                //向商品——规格项 表添加数据
                goodsMapper.addGoodsAndOption(goods.getId(),option.getId());
            }
        }

        //向消息队列中发送消息
        GoodsDesc goodsDesc = findGoodsDescById(goods.getId());
        DomainEvent event = new DomainEvent();
        event.setEventType("sync_goods");
        event.setData(goodsDesc);
        rocketMQTemplate.syncSendOrderly("goods_event_topic",event, String.valueOf(goods.getId()));
    }

    @Override
    public void update(Goods goods) {
        goodsMapper.updateById(goods);

        //删除原本的图片
        QueryWrapper<GoodsImage> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("goodsId", goods.getId());
        goodsImageMapper.delete(imageQueryWrapper);
        //添加新的图片
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goods.getId());
            goodsImageMapper.insert(image);
        }

        //删除该商品的规格项
        goodsMapper.deleteGoodsOption(goods.getId());
        //添加新的规格项
        List<Specification> specifications = goods.getSpecifications();
        for (Specification specification : specifications) {
            List<SpecificationOption> optionList = specification.getSpecificationOptions();
            for (SpecificationOption specificationOption : optionList) {
                goodsMapper.addGoodsAndOption(goods.getId(),specificationOption.getId());
            }
        }

        //向消息队列中发送消息
        GoodsDesc goodsDesc = findGoodsDescById(goods.getId());
        DomainEvent event = new DomainEvent(); //事件对象
        event.setEventType("sync_goods");
        event.setData(goodsDesc);
        rocketMQTemplate.syncSendOrderly("goods_event_topic",event, String.valueOf(goods.getId()));


        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoodId(goods.getId());
        cartGoods.setGoodsName(goods.getGoodsName());
        cartGoods.setPrice(goods.getPrice());
        cartGoods.setHeaderPic(goods.getHeaderPic());

        DomainEvent event2 = new DomainEvent(); //事件对象
        event2.setEventType("update_cart");
        event2.setData(cartGoods);
        rocketMQTemplate.syncSend("cart_event_topic",event2);
    }

    @Override
    public Goods findById(Long id) {
        return goodsMapper.findById(id);
    }

    @Override
    public void putAway(Long id, Boolean isMarketable) {
        Goods goods = goodsMapper.selectById(id);
        DomainEvent event = new DomainEvent(); //事件对象
        if(isMarketable) {
            //向消息队列中发送消息
            GoodsDesc goodsDesc = findGoodsDescById(goods.getId());
            event.setEventType("sync_goods");
            event.setData(goodsDesc);

        }else{
            // 发送下架事件（无需知道谁消费）
            event.setEventType("off_goods"); //事件为下架
            event.setData(goods.getId());

            DomainEvent event1 = new DomainEvent();
            event1.setEventType("del_cart");
            event1.setData(id);
            rocketMQTemplate.syncSend("cart_event_topic",event1);
        }
        rocketMQTemplate.syncSendOrderly("goods_event_topic",event, String.valueOf(goods.getId()));

        goods.setIsMarketable(isMarketable);
        goodsMapper.updateById(goods);
    }

    @Override
    public List<GoodsDesc> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public Page<Goods> search(Goods goods, int page, int size) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if(goods!=null){
            if(StringUtils.hasText(goods.getGoodsName())){
                wrapper.like("goodsName",goods.getGoodsName());
            }
            if(StringUtils.hasText(goods.getCaption())){
                wrapper.like("caption",goods.getCaption());
            }
        }
        return goodsMapper.selectPage(new Page<>(page,size), wrapper);
    }

    @Override
    public GoodsDesc findGoodsDescById(Long id) {
        return goodsMapper.findDesc(id);
    }
}
